//
//  LayerSwitcher.cpp
//  Jump
//
//  Created by Zach Wang on 14-5-22.
//
//

#include "LayerSwitcher.h"

void LayerSwitcher::switchTo(int n)
{
    CCASSERT( n < _layers.size(), "Invalid index in MultiplexLayer switchTo message" );
    
    Layer* _oldLayer = _layers.at(_enabledLayer);
    Layer* _newLayer = _layers.at(n);
    this->addChild(_newLayer);
    
    this->setTransition(_oldLayer,_newLayer);

    this->removeChild(_oldLayer, true);
    _enabledLayer = n;
    
    
}

void LayerSwitcher::switchToAndReleaseMe(int n)
{
    int enabledLayer = _enabledLayer;
    LayerSwitcher::switchTo(n);
    _layers.replace(enabledLayer, nullptr);
}

void LayerSwitcher::switchToWithSectorTransition(int n)
{
    CCASSERT( n < _layers.size(), "Invalid index in MultiplexLayer switchTo message" );
    
    Layer* _oldLayer = _layers.at(_enabledLayer);
    Layer* _newLayer = _layers.at(n);
    this->addChild(_newLayer);
    
    //向左90度
    float rotate = -90;
    
    if (n > _enabledLayer) {
        rotate = -rotate;
    }
    

    ActionInterval *rotation = (ActionInterval*)(Sequence::create
                                                 (
                                                  EaseElasticOut::create(RotateBy::create(1, rotate)),
                                                  nullptr
                                                  ));
    ActionInterval *rotation2 = rotation->clone();
    
    
    _oldLayer->setAnchorPoint(Point(0.5f, -0.5f));
    _newLayer->setAnchorPoint(Point(0.5f, -0.5f));
    // 让待进入的layer放在另一侧横躺
    _newLayer->setRotation(-rotate);
    
    _oldLayer->runAction(rotation);
    _newLayer->runAction
    (
     Sequence::create
     (
      rotation2,
      nullptr
      )
     );
    
    
    this->removeChild(_oldLayer, true);
    _enabledLayer = n;
}


LayerSwitcher::LayerSwitcher()
{

}

LayerSwitcher::~LayerSwitcher()
{
    LayerMultiplex::~LayerMultiplex();
}

bool LayerSwitcher::init()
{
    return LayerMultiplex::init();
}